import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { login } from '@/api/case'
const App = () => import('@/App.vue')

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
// 修改 原型对象中的push方法
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }
const routes = [{
  path: "/",
  component: App,
  // redirect: '/', //页面默认加载的路由
  children: [{
    path: "/",
    name: "compliantCasmgt",
    meta: {
      title: '案例管理',
      BottomNavShow: true
    },
    component: () => import("@/views/case/index.vue")
  },
  // 合规案例详情
  {
    path: "/compliantCasedetail",
    name: "compliantCasedetail",
    meta: {
      title: '案例详情',
      BottomNavShow: false
    },
    component: () => import("@/views/case/components/caseDetail.vue")
  },
  // 非合规人员案例详情
  {
    path: "/uncompCaseInfo",
    name: "uncompCaseInfo",
    meta: {
      title: '案例详情',
      BottomNavShow: false
    },
    component: () => import("@/views/case/components/uncompDetail.vue")
    },
  // 案例维度统计详情
  {
    path: "/caseStatisticsInfo",
    name: "caseStatisticsInfo",
    meta: {
      title: '统计详情',
      BottomNavShow: false
    },
    component: () => import("@/views/case/components/caseStatisticsDetail.vue")
    },
  // 员工维度统计详情
  {
    path: "/employeeStatisticsInfo",
    name: "employeeStatisticsInfo",
    meta: {
      title: '统计详情',
      BottomNavShow: false
    },
    component: () => import("@/views/case/components/employeeStatisticsDetail.vue")
  },
  {
    path: "/employeeProfile",
    name: "employeeProfile",
    meta: {
      title: '员工画像',
      BottomNavShow: true
    },
    component: () => import("@/views/profile/index.vue")
    },
    {
      path: "/profileDetail",
      name: "profileDetail",
      meta: {
        title: '员工详情',
        BottomNavShow: false
      },
      component: () => import("@/views/profile/detail.vue")
    },
    {
      path: "/assessment",
      name: "assessment",
      meta: {
        title: '知识测评',
        BottomNavShow: true
      },
      component: () => import("@/views/assessment/index.vue")
    }
  ]
}]

const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  base: (process.env.NODE_ENV == 'production') ? '/compliance/' : '/', // process.env.BASE_URL  /compliance
  scrollBehavior: () => ({
    y: 0
  }), //滚动到顶端
  routes
})

const appId = 'wx36b31857f8d20a72'
const scope = 'snsapi_base'
const urlState = 'Citics600030'
const agentId = '1000238'

// if(store.state.token)store.dispatch('set_menulist')
//
let wxcode, redirectUrl, openUrl

router.beforeEach((to, from, next) => {
  const token = store.state.token
  // console.log('router-index路由守卫: ',token);
  if (to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = '中信证券'
  }
  let ua = navigator.userAgent.toLowerCase();
  let isWx = ua.match(/MicroMessenger/i) == "micromessenger";

  if (ua.match(/WxWork/i) == "wxwork") {
    // Toast("企业微信环境")
    // wx.hideOptionMenu()
  } else if (isWx) {
    // Toast("微信环境")
    return
  } else {
    // Toast("其它")
    //  return
  }
  wxcode = '023451' // 合规
//   wxcode = '041889' //非合规
  // wxcode = '039685'
  // wxcode = 'T018890'

  // wxcode = '039685' //text-非合规
  // console.log('当前人员wxcode: ',wxcode);
  process.env.VUE_APP_MODE == 'development' ? wxcode = wxcode : wxcode = to.query.code
  redirectUrl = window.location.href
  if (token) {
    // console.log('有token情况');
    next()
  } else {
    // console.log('没有token情况');
    // redirectUrl 直接写首页的地址
    redirectUrl = encodeURIComponent(redirectUrl)
    openUrl =
      `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}&state=${urlState}&agentid=${agentId}#wechat_redirect`
    if (wxcode) {
      // console.log('有当前登录人');
      let params = {
        code: wxcode,
        agentId:agentId
      }
      login(params).then(async res => {
        await store.dispatch('set_userToken', res.data)
        await store.dispatch('set_menulist')
        next()
      })
    } else {
      window.location.href = openUrl
    }
  }
})
export default router